<?php
namespace App\Admin\Controllers;
use App\Admin\Models\H5page;
use Common\Common;
use Common\ControllerAdBase;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class H5pageController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function listAction(){
        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = 1;
        }else{
            $page = (int)$this->params['page'];
        }
        //当前条数
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $pageSize = 10;
        }else{
            $pageSize = (int)$this->params['limit'];
        }
        $h5page = new H5page();
        $list = $h5page->page_list($this->params);
        $paginator = new PaginatorModel(
            array(
                "data" => $list,
                "limit"=> $pageSize,
                "page" => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $list_items = Common::transition_array($list_items);
        foreach($list_items['items'] as $key=>$value){
            $list_items['items'][$key]['copy_url'] = Common::domain()."/api/h5page/h5page?type=".$value['type'];
        }
        $this->ok($list_items);
    }



    public function addpageAction(){
        if(empty($this->params['type'])){
            $this->fail(-1,'标识不能为空');
        }else{
            $h5page = new H5page();
            $is_type = $h5page->findFirstByType($this->params['type']);
            if($is_type){
                $this->fail(-1,'该标识已经存在，请更换');
            }
        }
        $notices_detail['title']=$this->params['title'];
        $notices_detail['type']=$this->params['type'];
        $notices_detail['type_name']=$this->params['type_name'];
        $notices_detail['content']=$this->params['content'];
        $h5page = new H5page();
        $result = $h5page->page_save($notices_detail);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'添加失败');
        }
    }


    public function updatepageAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'id不能为空');
        }
        $h5page = new H5page();
        $notices_detail = $h5page->page_detail($this->params['id']);
        if(empty($notices_detail)){
            $this->fail(-1,'该说明不存在或以被删除');
        }
        if(empty($this->params['type'])){
            $this->fail(-1,'标识不能为空');
        }else{
            $h5page = new H5page();
            $is_type = $h5page->count_type($this->params);
            if($is_type){
                $this->fail(-1,'该标识已经存在了，请更换');
            }
        }
        $notices_detail['title']=$this->params['title'];
        $notices_detail['type']=$this->params['type'];
        $notices_detail['type_name']=$this->params['type_name'];
        $notices_detail['content']=$this->params['content'];
        $result = $h5page->page_save($notices_detail);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'修改失败');
        }
    }


    public function detailpageAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'id不能为空');
        }
        $h5page = new H5page();
        $detail = $h5page->page_detail($this->params['id']);
        if($detail){
            $this->ok($detail);
        }else{
            $this->fail(-1,'该内容不存在或已被删除');
        }
    }

}
